<template>
    <!-- 付款弹窗 -->
    <div class="pay-panel">
      <van-popup v-model="isShow"  get-container="#app-container" closeable close-icon-position="top-left" position="bottom" :style="{ height: '60%' }">
        <div class="pay-title">确认付款</div>
        <p class="pay-amount">{{ bill.reductedAmount-bill.recieved }}元</p>
        <van-cell-group>
          <van-cell title="订单信息" value="水费" />
          <van-cell title="付款方式" value="支付宝支付" is-link />
        </van-cell-group>
        <div class="pay-button" @click="doPay">
          <van-button type="info" size="large">立即付款</van-button>
        </div>
      </van-popup>
    </div>

</template>

<script>
import 'vant/lib/index.css';

import {Button, Toast,Cell, CellGroup,Popup,Divider} from "vant";


export default {
  name: "Unpaid",
  components: {
    [Button.name]: Button,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Divider.name]: Divider,

  },
  data() {
    return {
      token: localStorage.getItem("token"),
      billList: [
        //账单列表
        {
          // no: 2017834,
          // name: '王小明',
          // accountPeriod: '2022-12',
          // consumption: 120,
          // reductedAmount: 500,
        },
      ],
      isShow: false, //显示弹窗
      bill: {}, //传输对象
      charge: { id: "", recieved: "" },
    };
  },

  methods: {
    //前往付款
    goPay(val) {
      this.bill = val;
      this.charge.id = this.bill.chargeId;
      this.charge.recieved = this.bill.reductedAmount;
      this.isShow = true;
    },
    //付款
    doPay() {
      Toast.loading({
        message: "支付中...",
        forbidClick: true,
      });
      setTimeout(() => (this.isShow = false), 2000);
      setTimeout(() => {
        this.charge.recieved = this.bill.reductedAmount; 
        this.$axios
          .post("api/charge/commitCharge", this.charge, {
            headers: { token: this.token },
          })
          .then((resp) => {
            if (resp.data.code == 200) {
              Toast.success({ message: "支付成功" });
              this.queryList();
            } else {
              this.$message.error("入账失败");
              this.queryList();
            }
          });
      }, 2500);
    },
    //请求列表
    queryList() {
      this.$axios
        .get("/api/bill/bill-unpaid", { headers: { 'token': this.token } })
        .then((resp) => {
          console.log(resp.data);
          if (resp.data.code === 200) {
            this.billList = resp.data.data;
          } else {
            Toast("列表加载失败");
          }
        });
    },
    //发送电子发票
    sendEmail() {
      setTimeout(() => {
        Toast.success("发送成功");
      }, 500);
    },
    //更新缴费信息
    updateReceived() {
      this.$axios.post("/api/");
    },
  },

  created() {
    this.queryList();
  },
};
</script>

<style scoped>
.unpaid-container {
  width: 100vw;
  min-height: 100%;
  font-family: “Microsoft YaHei”;
  padding: 0 0 65px 0;
}

.bill-card {
  height: 160px;
  background-color: white;
  margin-left: 10px;
  margin-right: 10px;
  margin-top: 10px;
  border-radius: 10px;
  padding: 10px 20px;
}

.bill-title {
  font-size: 20px;
  font-weight: bold;
  padding: 5px 0;
  margin-bottom: 5px;
  justify-content: space-between;
  display: flex;
}

.bill-time {
  font-size: small;
  color: grey;
  padding: 5px 0;
  margin-bottom: 5px;
}

.bill-data {
  justify-content: space-between;
  display: flex;
  padding: 5px 0;
  font-size: 14px;
}

.bill-button {
  justify-content: space-between;
  display: flex;
  padding: 0px 30px;
  font-size: 14px;
  color: rgb(36, 148, 228);
}

.pay-panel {
  font-family: "Microsoft YaHei";
}

.pay-title {
  height: 50px;
  background-color: whitesmoke;
  line-height: 50px;
  text-align: center;
}

.pay-amount {
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 40px;
}

.pay-button {
  margin-top: 20px;
  padding: 0 20px;
  display: flex;
  justify-content: center;
}
</style>